﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Tauren.Business.DLL.TenantInformation;
using Tauren.Business.Entity.Config;
using Tauren.Business.Entity.Enum;

namespace Tauren.Business.Service.TenantInformation
{
    /// <summary>
    /// 租户业务逻辑处理
    /// </summary>
    public class TenantInformationService : ITenantInformationService
    {
        private IMapper mapper { get; set; }
        public ITenantInformationBusiness business { get; set; }
        public TenantInformationService(IMapper _mapper)
        {
            mapper = _mapper;
        }

        /// <summary>
        /// 新增租户
        /// </summary>
        /// <param name="tenant"></param>
        /// <returns></returns>
        public IResponseMessage Add(Entity.Input.TenantInformation_Input tenant)
        {
            Entity.Model.TenantInformation model = mapper.Map<Entity.Input.TenantInformation_Input, Entity.Model.TenantInformation>(tenant);
            model.CreatDate = DateTime.Now;
            Entity.Model.TenantInformation information = business.GetByCode(model.Code);
            if (information == null)
                return business.Add(model).Succeed();
            else return ErrorType.BUSINESS_TENANTINFO_REPEAT_CODE.Failure(ErrorMessage.BUSINESS_TENANTINFO_REPEAT_CODE);
        }

        /// <summary>
        /// 编辑租户
        /// </summary>
        /// <param name="tenant"></param>
        /// <returns></returns>
        public IResponseMessage Editor(Entity.Input.TenantInformation_Input tenant)
        {
            Entity.Model.TenantInformation model = mapper.Map<Entity.Input.TenantInformation_Input, Entity.Model.TenantInformation>(tenant);
            Entity.Model.TenantInformation information = business.Get(model.AutoId);
            if (information == null)
                return ErrorType.BUSINESS_NOT_TENANTINFO.Failure(ErrorMessage.BUSINESS_NOT_TENANTINFO);
            else
                return business.Editor(model).Succeed();
        }

        /// <summary>
        /// 删除租户
        /// </summary>
        /// <param name="auotId"></param>
        /// <returns></returns>
        public IResponseMessage Delete(int auotId) => business.Delete(auotId).Succeed();

        /// <summary>
        /// 删除租户--根据租户ID
        /// </summary>
        /// <param name="auotId"></param>
        /// <returns></returns>
        public IResponseMessage DeleteByCode(string code) => business.DeleteByCode(code).Succeed();

        /// <summary>
        /// 根据Id获取数据
        /// </summary>
        /// <param name="autoId"></param>
        /// <returns></returns>
        public IResponseMessage Get(int autoId)
        {
            Entity.Model.TenantInformation model = business.Get(autoId);
            Entity.Output.TenantInformation_Output output = mapper.Map<Entity.Model.TenantInformation, Entity.Output.TenantInformation_Output>(model);
            if (output == null) output = new Entity.Output.TenantInformation_Output();
            return output.Succeed();
        }

        /// <summary>
        /// 根据code获取数据
        /// </summary>
        /// <param name="autoId"></param>
        /// <returns></returns>
        public IResponseMessage GetByCode(string code)
        {
            Entity.Model.TenantInformation model = business.GetByCode(code);
            Entity.Output.TenantInformation_Output output = mapper.Map<Entity.Model.TenantInformation, Entity.Output.TenantInformation_Output>(model);
            if (output == null) output = new Entity.Output.TenantInformation_Output();
            return output.Succeed();
        }

        /// <summary>
        /// 获取所有的租户信息
        /// </summary>
        /// <param name="autoId"></param>
        /// <returns></returns>
        public IResponseMessage All()
        {
            List<Entity.Model.TenantInformation> model = business.All();
            List<Entity.Output.TenantInformation_Output> output = mapper.Map<List<Entity.Model.TenantInformation>, List<Entity.Output.TenantInformation_Output>>(model);
            return output.Succeed();
        }
    }
}
